package cn.edu.shiep.aurora.gunwar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.edu.shiep.aurora.gunwar.rule.RuleServer;
import cn.edu.shiep.aurora.gunwar.sensor.MediaPlay;
import cn.edu.shiep.aurora.gunwar.sensor.Vibrating;
import cn.edu.shiep.aurora.gunwar.weapen.GunOperatorEventListener;
import cn.edu.shiep.aurora.gunwar.weapen.GunOperatorEvent;
import cn.edu.shiep.aurora.gunwar.weapen.Gun;
import cn.edu.shiep.aurora.gunwar.weapen.WeaponSever;

public class World extends Activity implements GunOperatorEventListener {
	static int count = 0;
	boolean press = true;
	MediaPlay media;
	WeaponSever sever;
	Vibrating vib;
	ImageView[] imageView = new ImageView[6];
	LinearLayout bullets;
	TextView tv;
	RuleServer rule;
	public TextView debugText;
	ImageButton fireButton;
	public void init() {
		media = MediaPlay.getInstance(this);
		sever = new WeaponSever(this,  new Gun());
		sever.addGunOperatorEventListener(this);
		vib = new Vibrating();
		rule=new RuleServer(this);
		rule.init(media, sever, vib);

	}

	// ____________________________________________
	public void operator(GunOperatorEvent e) {

		if (press) {

			switch (e.getType()) {
			case OPEN_FIRE:
				vib.vibrate(this);
				media.playMusicShootAGun(this);
				if (count == 6) {
					count = 0;
				}
				bullets.getChildAt(count).setVisibility(View.INVISIBLE);
				count++;
				
				break;

			case RELOAD:
				media.playB14Change(this);
				for (ImageView i : imageView)
					i.setVisibility(View.VISIBLE);
				count = 0;
				break;

			case NOAMAO:
				media.playNoAMOM(this);
				break;
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gunlayout);

		tv = (TextView) findViewById(R.id.tvpress);
		MediaPlay.getInstance(this).stopBGM();
		init();
		debugText=(TextView)findViewById(R.id.debugText);
		if(((MyApplication)getApplication()).ISDEBUG)
			debugText.setVisibility(View.VISIBLE);
		
		imageView[0] = (ImageView) findViewById(R.id.bullet0);
		imageView[1] = (ImageView) findViewById(R.id.bullet1);
		imageView[2] = (ImageView) findViewById(R.id.bullet2);
		imageView[3] = (ImageView) findViewById(R.id.bullet3);
		imageView[4] = (ImageView) findViewById(R.id.bullet4);
		imageView[5] = (ImageView) findViewById(R.id.bullet5);
		bullets = (LinearLayout) findViewById(R.id.bullets);
		fireButton=(ImageButton)findViewById(R.id.fireButton);
		fireButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				sever.fire(1000,System.currentTimeMillis()*1000);
			}
		});
		tv.setOnLongClickListener(new OnLongClickListener() {

			public boolean onLongClick(View v) {
				press = true;
				return true;
			}
		});
		tv.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				press = false;
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		sever.resume(this);
		rule.resume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		sever.pause();
		rule.pause();
	}
}
